<?php
//ini_set( 'display_errors','on' );
//error_reporting( E_ALL );

require_once('entry.php');
require_once('config/config.inc.php');

$valid_get = array( 'index', 'search', 'item', 'category', 'updateImgUrl', 'newest', 'hotest', 'updateItemDetail', 'item_new', 'movie_list', 'advanced_search', 'search_typeahead' );
$valid_post = array( 'search' );
StartAPP();

function StartAPP(){
    assignCommonVars();
    if( $_SERVER['REQUEST_METHOD'] == 'POST' )
    {
        doPost();
    }
    else if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
        doGet();
    }
}

function doGet(){
    global $valid_get;
    $action = $_GET['action'];

    if( empty($action) ){
        $action = 'index';
    }

    if( in_array( $action, $valid_get ) && class_exists( $action ) ){
        $act = new $action();
        $act->run();
    }
    else{
        echo "undefined action";
    }
}

function doPost(){
    global $valid_post;
    $action = $_POST['action'];

    if( in_array( $action, $valid_post ) && class_exists( $action ) ){
        $act = new $action();
        $act->run();
    }
    else{
        echo "undefined action";
        die;
    }
}

/**
 * assign page common content
 * @global  $TPL
 * @global <type> $categoryMap
 * @param <type> $action
 */
function assignCommonVars(){
    global $TPL, $categoryMap;
    $TPL->assign( 'categories', $categoryMap );
    if( $_SERVER['REQUEST_METHOD'] == 'POST' )
    {
        $action = cleanInput( $_POST['action'] );
    }
    else if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
        $action = cleanInput( $_GET['action'] );
    }

    if( $action != 'index' && !empty($action) ){
        $TPL->assign( 'breadcrum', generateBreadCrum($action) );
    }
}

// get related post
// algorithm
// 1. if contains S[0-9]{1,2}, remove unnessary chars, and using like
// 2. else, get random item from same category

?>
